package top.yongyan0624.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import top.yongyan0624.Utils.RedisUtil;
import top.yongyan0624.controller.CountController;
import top.yongyan0624.controller.StarController;
import top.yongyan0624.entity.Count;
import top.yongyan0624.entity.Message;
import top.yongyan0624.entity.Star;
import top.yongyan0624.entity.SysUser;
import top.yongyan0624.mapper.MessageMapper;
import top.yongyan0624.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.yongyan0624.service.SysUserService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yongyan
 * @since 2021-05-17
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private CountController countController;

    @Autowired
    private StarController starController;

    @Override
    public Page<Message> getMessage(Long postId, Integer pageNum, Integer pageSize) {
        Page<Message> page = new Page<>(pageNum, pageSize);

        Page<Message> pageData = this.page(page, new QueryWrapper<Message>().orderByDesc("time"));

        return messageCache(postId, pageData);
    }

    @Override
    public Page<Message> getMessageById(Long postId, Integer pageNum, Integer pageSize) {
        Page<Message> page = new Page<>(pageNum, pageSize);

        Page<Message> pageData = this.page(page, new QueryWrapper<Message>().eq("user_id",postId).orderByDesc("time"));

        return messageCache(postId, pageData);
    }

    /**
     * 对pageData里面的所有message数据进行同步缓存的处理
     * @param postId
     * @param pageData
     * @return
     */
    public Page<Message> messageCache(Long postId, Page<Message> pageData) {
        Count count = new Count();
        Star star = new Star();
        //处理message里面关于点赞、收藏、以及和用户相关的数据
        for (Message message : pageData.getRecords()) {
            //缓存
            if (redisUtil.hasKey("count:" + message.getId())) {
                //若有点赞缓存，直接使用
                message.setCount((Integer) redisUtil.get("count:" + message.getId()));
            } else {
                //若没有缓存,做缓存
                redisUtil.set("count:" + message.getId(), message.getCount());
            }

            //star也同样处理
            if (redisUtil.hasKey("star:" + message.getId())) {
                message.setStar((Integer) redisUtil.get("star:" + message.getId()));
            } else {
                redisUtil.set("star:" + message.getId(), message.getStar());
            }

            if (postId != null) {
                //查询关于该postId的状态
                count.setMsgId(message.getId());
                count.setUserId(message.getUserId());
                count.setPostId(postId);
                Count c = (Count) countController.getCountById(count).getData();
                if (c != null) {
                    message.setCountStatus(c.getStatus());
                }
                star.setMsgId(message.getId());
                star.setUserId(message.getUserId());
                star.setPostId(postId);
                Star s = (Star) starController.getStarById(star).getData();
                if (s != null) {
                    message.setStarStatus(s.getStatus());
                }
            }

            SysUser user = sysUserService.getOne(new QueryWrapper<SysUser>().eq("id", message.getUserId()));
            if (user != null) {
                message.setName(user.getUsername());
                message.setHeadPortrait(user.getAvatar());
                message.setEmail(user.getEmail());
                message.setPhone(user.getPhone());
            }
        }

        return pageData;
    }
}
